(function($) {

    $(document).ready(function() {
        var $canvas = $("#canvasParty");
        var $coord = $("#mousePosition");
        var pos = $canvas.offset();
        var ctx = $canvas.get(0).getContext("2d");
        ctx.fillStyle = "rgba(255,0,0,0.1)";

        var explosoes = [];

        setInterval(function() {
            ctx.clearRect(0, 0, 500, 500);
            for (var i = 0; i < explosoes.length; i++) {
                ctx.beginPath();
                ctx.fillStyle = "rgba(255,0,0," + explosoes[i].opacity + ")";
                console.log(JSON.stringify(explosoes[i]));
                ctx.arc(explosoes[i].x, explosoes[i].y, 30, 0, 2 * Math.PI);
                ctx.fill();
                explosoes[i].opacity -= 0.01;
                if (explosoes[i].opacity < 0.01) {
                    explosoes.splice(i, 1);
                }
            }

        }, 10);

        $canvas.on("mousemove", function(e) {

            $coord.text("x: " + (e.pageX - pos.left) + ",  y: " + (e.pageY - pos.top));
        }).on("click", function(e) {
            var x = e.pageX - pos.left + 12, y = e.pageY - pos.top + 12;
            explosoes.push({
                x: x,
                y: y,
                opacity: 1
            });

        });


        // ----------------------------------------

        // Create the Socket
        var socketId;
        console.log("criando socket");
        chrome.socket.create('udp', function(socketInfo) {
            console.log('v');
            // The socket is created, now we want to connect to the service
            socketId = socketInfo.socketId;
            console.log(socketId);
            //TODO colocar o IP do CLIENTE!
            chrome.socket.bind(socketId, "192.168.0.19", 8088, function() {
                console.log('bind');
                
                chrome.socket.recvFrom(socketId, function(data){
                    console.log(ab2str(data.data));
                });
                
                chrome.socket.joinGroup(socketId, "230.185.192.108", function() {
                    console.log('join');
                    chrome.socket.getJoinedGroups(socketId, function(groups){
                        console.log(JSON.stringify(groups));
                    });
                    
                    chrome.socket.sendTo(socketId, str2ab("alo do Chrome!!"), "230.185.192.108", 8088, function() {
                        console.log('send');
                    });
                });
            });


//                    chrome.socket.connect(socketId, function(result) {
//                        // We are now connected to the socket so send it some data
//                        chrome.socket.write(socketId, arrayBuffer,
//                                function(sendInfo) {
//                                    console.log("wrote " + sendInfo.bytesWritten);
//                                }
//                        );
//                    });
        }
        );

    });



    

})(jQuery);

function ab2str(buf) {
        return String.fromCharCode.apply(null, new Uint16Array(buf));
    }

    function str2ab(str) {
        var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
        var bufView = new Uint16Array(buf);
        for (var i = 0, strLen = str.length; i < strLen; i++) {
            bufView[i] = str.charCodeAt(i);
        }
        return buf;
    }